import axios from 'axios'
// axios.defaults.withCredentials = true
import Vue from 'vue'
import router from '@/router'
import App from '@/App'
import { Message } from 'element-ui'
//  axios.defaults.baseURL = 'http://192.168.5.51:8071'

// axios.defaults.baseURL = 'http://127.0.0.1:8080/'
// axios.defaults.baseURL = '/api'

axios.defaults.baseURL = 'http://127.0.0.1:8080/'
// axios.defaults.headers.post['Content-Type'] = 'application/json';

const service = axios.create({
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})
// axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
// axios.defaults.headers.post['Content-type'] = 'application/x-www-form-urlencoded'

// request interceptor
service.interceptors.request.use(
  config => {
    // if (store.getters.token) {
    //   config.headers = HEADER
    // config.headers['Authorization'] = 'Bearer ' + store.getters.token
    // }
    // console.log(config)
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    return response
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.message || '出现错误',
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

/**
 * @description get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params) {
  return new Promise((resolve, reject) => {
    service.get(url, {
      params: params || {}
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error)
    })
  })
}

/**
 * @description delete方法，对应delete请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function del(url, params) {
  return new Promise((resolve, reject) => {
    service.delete(url, {
      data: params
    }).then(res => {
      resolve(res.data)
    }).catch(error => {
      reject(error)
    })
  })
}

/**
 * @description post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function post(url, params) {
  return new Promise((resolve, reject) => {
    service.post(url, params)
      .then(res => {
        resolve(res.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}
/**
 * @description postImage方法，对应postImage请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function postImage(url, params) {
  return new Promise((resolve, reject) => {
    service.post(url, params, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
      .then(res => {
        resolve(res.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}

/**
 * @description put方法，对应put请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function put(url, params) {
  return new Promise((resolve, reject) => {
    service.put(url, params)
      .then(res => {
        resolve(res.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}

const request = {}
request['get'] = (url, params) => get(url, params)
request['del'] = (url, params) => del(url, params)
request['post'] = (url, params) => post(url, params)
request['postImage'] = (url, params) => postImage(url, params)
request['put'] = (url, params) => put(url, params)
export default request
